package com.itheima.demo.utils;

import io.jsonwebtoken.JwtBuilder;
import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.SignatureAlgorithm;

import java.util.Date;
import java.util.UUID;

public class JwtToken {

    //存活时间毫秒值
    private static long time = 1000 * 60 * 60;
    //定义解析密匙
    private static String signature = "admin";
    public static String jwt() {
        //创建jwt加密器
        JwtBuilder jwtBuilder = Jwts.builder();
        String jwtToken = jwtBuilder
                //定义jwt头文件
                .setHeaderParam("typ", "jwt")
                .setHeaderParam("alg", "hs256")
                //定义jwt的荷载
                .claim("username", "张三")
                .claim("role", "admin")
                .setSubject("admin-test")
                //设置死亡时间（当前系统时间+存活时间）
                .setExpiration(new Date(System.currentTimeMillis() + time))
                //设置随机UUID
                .setId(UUID.randomUUID().toString())
                //定义加密令牌锁
                .signWith(SignatureAlgorithm.HS256, signature)
                .compact();
        return jwtToken;
    }
}
